package com.videoshare.user.dao;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import java.io.InputStream;
import org.apache.commons.logging.*;


public class BaseDAO 
{
	private static Log log = LogFactory.getLog(BaseDAO.class);
    
    /**
     * Preleva i dati del result set contenuti in un oggetto List e li salva in un 
     * bean appropriato, facendo corrispondere un bean a ciascun record.
     */
    //abstract List makeBeanList(List rows);
    
    
    /**
     * Copia i record del result set passato come argomento in un oggetto List. Per 
     * ogni record costruisce una lista contenente il nome del campo, il tipo di dato
     * e il valore.
     */
    protected List resultsetToList(ResultSet rs) throws SQLException
    {
        int rowsCount = 0;
        ResultSetMetaData rsmd = null;
        List row = new ArrayList();
        
        rsmd = rs.getMetaData();
        
        String tableName = rsmd.getTableName(1);
        
        int cols = rsmd.getColumnCount();
            
            while(rs.next()){
                
                rowsCount++;

                List fields = new ArrayList();
                for(int i = 0; i < cols; i++){
                    String columnClassName = rsmd.getColumnClassName(i + 1);
                    //System.out.print("User column data type: " + columnClassName + " ");
                    TableFieldHandler fieldhandler;
                    if( !columnClassName.equals("java.lang.Object") )
                    {
                        Object object = rs.getObject(i + 1);
                        String columnName = rsmd.getColumnName(i + 1);     
                        log.debug("Table " + tableName + ", column " + columnName + " of type " + columnClassName);
                        //System.out.println("column name: " + columnName + " value: " + object.toString());
                        fieldhandler = new TableFieldHandler(columnClassName, object, columnName); 
                    }
                    else 
                    {
                        java.sql.Blob blob = rs.getBlob(i + 1);
                        
                        String columnName = rsmd.getColumnName(i + 1);  
                        log.debug("Table " + tableName + ", column " + columnName + " of type " + columnClassName);
                        InputStream blobstream = blob.getBinaryStream();
                        fieldhandler = new TableFieldHandler(columnClassName,
                                                                               blobstream, 
                                                                               columnName);
                    }
                    
                    fields.add(fieldhandler);
                    
                }
                
                row.add(fields);
                
            }
            
        
        return row;
    }
}


